<?php

namespace Qm;


function replaceFunctions( $sLine ) {

    // no pipeline == no function is called here
    if( FALSE === strpos( $sLine, '|' ) ) {
        return $sLine;
    }

    $sTranslatedLine    = '';
    $iLineLength        = strlen( $sLine );
    $sVarPattern        = '/^(?<var>\$[\w\_][\w\_\d]*(?:\.[\w\_][\w\_\d]*)*)\|/';
    $sArgsPattern =
        '/^\s*
            (?<fname>[\w\_][\w\_\d]*)
            (?:
                \:
                (?<arg1>
                    (?<primitive>
                        null |
                        false |
                        true |
                        (?:[+-]?0x[0-9a-fA-F]+) |
                        [+-]?\d+(?:\.\d+)? |
                        "[^"\\\\]*(?:\\\\.[^"\\\\]*)*" |
                        \'[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*\'
                    ) |
                    (?<variable>\$[\w\_][\w\_\d]*(?:\.[\w\_][\w\_\d]*)*) |
                    (?<constant>\#[\w\_][\w\_\d]*)
                )
                (?:
                    \,
                    (?<arg2>
                        (?&primitive) |
                        (?&variable) |
                        (?&constant)
                    )
                )?

            )?
        /iSx';

    // we are looking for variables with pipelines
    for( $i = 0; $i < $iLineLength; ++$i ) {
    if( $sLine[$i] != '$' ) {
            $sTranslatedLine .= $sLine[$i];
        } elseif( preg_match( $sVarPattern, substr( $sLine, $i ), $aMatches ) ) {

            $sVariableContent   = $aMatches['var'];
            $sVariableLength    = strlen( $sVariableContent );

            while( preg_match( $sArgsPattern, substr( $sLine, $i + $sVariableLength + 1 ), $aArgMatches ) ) {
                $sVariableLength    += strlen( $aArgMatches[0] ) + 1;
                $sVariableContent   = $aArgMatches['fname'] . '('
                    . $sVariableContent
                    . ( isset( $aArgMatches['arg1'] ) ? ',' . $aArgMatches['arg1'] : NULL )
                    . ( isset( $aArgMatches['arg2'] ) ? ',' . $aArgMatches['arg2'] : NULL )
                . ')';

                if( ! isset( $sLine[$i + $sVariableLength] ) || $sLine[$i + $sVariableLength] != '|' ) {
                    break;
                }

            }
            $i += $sVariableLength - 1;
            $sTranslatedLine .= $sVariableContent;
        } else {
            $sTranslatedLine .= $sLine[$i];
        }
    }

    return $sTranslatedLine;
}


require_once( 'exceptions.class.php' );
require_once( 'family.class.php' );
require_once( 'query.class.php' );
require_once( 'filecontent.class.php' );
require_once( 'fileparser.class.php' );
require_once( 'contentparser.class.php' );
require_once( 'tplfunctions.php' );

$a = new \Qm\FileParser;
$a->setFile( 'file.tpl' );
$a->parseFile();
$a->dump();

$oFile = $a->getParsedFile();
$b = new \Qm\ContentParser;
$b->setFileContent($oFile);
$b->parseFileContent();
